import ruleOpt from '@/biz/option/rule.js'
import templateMgr from '@/biz/bonus/template.js'
import toolMgr from '@/biz/base/tool.js'
let _this = null

function rules() {
  return _this.info.auditResultFlag == '2' ? [
    ruleOpt.required('请填写审批内容')
  ] : []
}

function fileAttachment(info) {
  let api = toolMgr.unloadFile()
  let files = []
  return {
    title: '文件附件',
    key: 'fileList',
    placeholder: '(最大支持20M文件，若文件过大请压缩或分多个文件)',
    upload: {
      title: '文件附件',
      api,
      files,
      multiple: true,
      show: false,
      showDel: true,
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      }
    },
    data: {
      to: (v) => {
        let fileList = v.map(item => {
          return {
            accessoryName: item.name,
            name: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          fileList
        }
      }
    }
  }
}

function picAttachment(info) {
  let api = toolMgr.unloadFile()
  let files = []
  return {
    title: '图片附件',
    key: 'picList',
    uploadImg: {
      title: '图片附件',
      api,
      files,
      multiple: false,
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: false,
      maxlength: 10,
      format: ['jpg', 'jpeg', 'png'],
    },
    data: {
      to: (v) => {
        let picList = v.map(item => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
          }
        })
        return {
          picList
        }
      }
    }
  }
}

function options(vm, flag) {
  _this = vm
  let opt = [
    [{
      title: '审批结果',
      key: 'auditResultFlag',
      placeholder: '请选择',
      select: {
        options: [
          { key: '1', value: '通过' },
          { key: '2', value: '不通过' },
        ]
      },
      associationRules: ['auditReason'],
      change: () => {
        opt[1][0].rules = rules()
      },
      rules: [
        ruleOpt.required('请选择审批结果'),
      ]
    }],
    [{
      title: '审批内容',
      key: 'auditReason',
      input: {
        type: 'textarea',
        autosize: {
          minRows: 4
        },
        maxlength: 200
      },
      rules: rules()
    }],

  ]
  if (_this.info.picList) {
    opt.push([picAttachment()])

  }
  if (_this.info.fileList) {
    opt.push([fileAttachment()])
  }
  return opt
}

export default {
  options
}